package us.wili.dev.platform;


import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import us.wili.dev.common.mybatis.IdAndTimeAutoUpdatePlugin;
import us.wili.dev.common.util.snowflake.SnowflakeUtil;
import us.wili.dev.platform.filter.AlwaysCreateJwtTokenFilter;

import javax.servlet.MultipartConfigElement;


@SpringBootApplication(
        exclude = {SecurityAutoConfiguration.class},
        scanBasePackages = {"us.wili.dev"}
)
@MapperScan({"us.wili.dev.dao", "us.wili.dev.platform.dao"})
@EnableScheduling
public class PlatformApplication {
    public static void main( String[] args ) {
        SpringApplication application = new SpringApplication(PlatformApplication.class);
        ConfigurableApplicationContext context = application.run(args);
    }

    /**
     * Mybatis插件，用于生成ID、版本号、创建时间、更新时间
     * @param idWorker
     * @return
     */
    @Bean
    public IdAndTimeAutoUpdatePlugin idAndTimeAutoUpdatePlugin(SnowflakeUtil idWorker,SnowflakeUtil versionWorkder) throws Exception {
        return new IdAndTimeAutoUpdatePlugin(idWorker, versionWorkder);
    }

    /**
     * 文件上传配置
     * @return
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //文件最大
        factory.setMaxFileSize("10240KB"); //KB,MB
        /// 设置总上传数据总大小
        factory.setMaxRequestSize("102400KB");
        return factory.createMultipartConfig();
    }
    /**
     * token 生成策略过滤器
     * @return
     */
    @Bean
    public AlwaysCreateJwtTokenFilter alwaysCreateJwtTokenFilter() {
        return new AlwaysCreateJwtTokenFilter();
    }
    @Bean
    public FilterRegistrationBean alwaysCreateJwtTokenFilterRegistrationBean(AlwaysCreateJwtTokenFilter alwaysCreateJwtTokenFilter) {
        FilterRegistrationBean registBean = new FilterRegistrationBean();
        registBean.setFilter(alwaysCreateJwtTokenFilter);
        registBean.addUrlPatterns("/*");
        //order的数值越小 则优先级越高
        registBean.setOrder(6);
        return registBean;
    }
}
